package org.emet.jee.console.entity.sys;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.emet.jee.console.entity.BaseEntity;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.hibernate.annotations.Filter;
import org.hibernate.annotations.FilterDef;
import org.hibernate.annotations.Index;
import org.hibernate.annotations.ParamDef;

@Table(name = "SYS_DATA_DIC")
@Entity
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
@FilterDef(name = "allValidItemsFilter", parameters = { @ParamDef(type = "boolean", name = "_isEnabled") })
public class DataDic extends BaseEntity {

	private String name;
	private String description;
	private Set<DataItem> items = new HashSet<DataItem>();
	private Boolean removable = Boolean.FALSE;

	public Boolean getRemovable() {
		return removable;
	}

	public void setRemovable(Boolean removable) {
		this.removable = removable;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	@Index(name = "sys_data_dic_name_index")
	@Column(unique = true)
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@OneToMany(mappedBy = "parent", fetch = FetchType.EAGER)
	@Fetch(FetchMode.SUBSELECT)
	@OrderBy("seq asc")
	@Filter(condition = "enabled_item = :_isEnabled", name = "allValidItemsFilter")
	public Set<DataItem> getItems() {
		return items;
	}

	public void setItems(Set<DataItem> items) {
		this.items = items;
	}

	@Override
	public String toString() {
		return ToStringBuilder.reflectionToString(this);
	}

}
